﻿using BJ.AuthEntity;
using Syspetro.Core.AppDeal;
using Syspetro.Core.CommandBus;
using Syspetro.Core.SysInterface;
using Syspetro.Deploy.Managers;
using System;

namespace BJ.AuthDomain.Commands
{
    /// <summary>
    /// 发送验证（需要登录）
    /// </summary>
    public class SendCodeCommand : Command
    {
        public AccountType AccountType { get; set; }
        public string Account { get; set; }
        public Guid UserId { get; set; }
        public bool IsAdmin { get; set; }

    }
    public interface ISendCodeCommandHandler : ICommandHandler<SendCodeCommand>, ITransientService
    {

    }
    public class SendCodeCommandHandler : CommandHandler<SendCodeCommand>, ISendCodeCommandHandler
    {
        readonly ICommandBus _command;
        readonly IAuthorizationManager _authorization;
        public SendCodeCommandHandler(ICommandBus command, IAuthorizationManager authorization)
        {
            _command = command;
            _authorization = authorization;
        }
        public override SpActionResult<string> Execute(SendCodeCommand command)
        {
            if (command.UserId == Guid.Empty)
            {
                command.UserId = _authorization.GetUserId;
                command.IsAdmin = _authorization.IsAdmin;
            }
            var res = command.AccountType switch
            {
                AccountType.Email => _command.Send(new SendEmailCommandUser { UserId = command.UserId, IsAdmin = command.IsAdmin }),
                AccountType.SMS => _command.Send(new SendSMSCommandUser { UserId = command.UserId, IsAdmin = command.IsAdmin }),
                _ => new SpActionResult { Success = false, Msg = "类型错误" },
            };
            return new SpActionResult<string> { Success = res.Success, Msg = res.Msg };
        }
    }
}
